/*
 * DiscoverySearchFragment.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.search;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.ArticleTitle;
import com.yidiandao.business.data.recyclertype.SearchAccurateContent;
import com.yidiandao.business.data.recyclertype.SearchHistory;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoverySearchAccurateProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoverySearchArticleProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoverySearchHistoryProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.DiscoverySearchTitleProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 发现页-搜索-一级页面
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/12 0012 18:24
 */
public class DiscoverySearchFragment extends SuperBaseFragment implements DiscoverySearchContact.View, DiscoverySearchHistoryProvider.OnHistoryItemClickListener {

    private DiscoverySearchContact.Presenter mPresenter;

    private TextView mTvCancel;

    private EditText mEtSearch;

    private RecyclerView mRecyclerView;

    private MultiTypeAdapter mAdapter;

    public static DiscoverySearchFragment getInstance() {
        return new DiscoverySearchFragment();
    }

    @Override
    protected void initViews() {
        mTvCancel = (TextView) mRootView.findViewById(R.id.tv_cancel);
        mEtSearch = (EditText) mRootView.findViewById(R.id.et_search);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);

        initRecyclerViews();
        initListeners();
    }

    private void initListeners() {
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        mEtSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && !TextUtils.isEmpty(getKeyword())) {
                    mPresenter.search(getKeyword());
                }
                return false;
            }
        });
    }

    @Override
    public String getKeyword() {
        return mEtSearch.getText().toString().trim();
    }

    private void initRecyclerViews() {
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(SearchAccurateContent.class, new DiscoverySearchAccurateProvider(mActivity));
        mAdapter.register(SearchHistory.class, new DiscoverySearchHistoryProvider(this));
        mAdapter.register(Article.class, new DiscoverySearchArticleProvider(mActivity));
        mAdapter.register(ArticleTitle.class, new DiscoverySearchTitleProvider(mActivity));

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discovery_search;
    }

    @Override
    public void setPresenter(DiscoverySearchContact.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return DiscoverySearchFragment.class.getSimpleName();
    }

    @Override
    public void setItems(List<?> items) {
        mAdapter.setItems(items);
        notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetChanged() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void onClearHistory() {
        mPresenter.clearHistory();
    }

    @Override
    public void onTagClick(String tag) {
        mEtSearch.setText(tag);
        mPresenter.search(tag);
    }
}
